package com.foolishbird.sdk.handler;

import android.os.Handler;
import android.os.Looper;
import android.os.Message;


public abstract class AsyncResponseHandler {

    private final AsyncResponseHandlerProxy handlerProxy;

    public AsyncResponseHandler() {
        handlerProxy = new AsyncResponseHandlerProxy(Looper.getMainLooper());
    }


    public final ResponseHanderInterface getResponseHanderInterface(){
        return handlerProxy;
    }

    public abstract void onAsyncSendRequest(int flag);

    public abstract void onAsyncResponseSuccess(int flag, String response);

    public abstract void onAsyncCancel(int flag);

    public abstract void onAsyncException(int flag, Exception ex);

    public abstract void onAsyncNetworkException(int flag, Exception ex);


    class AsyncResponseHandlerProxy extends Handler implements ResponseHanderInterface {

        private static final int ASYNC_MSG_SEND_REQUEST = 0x0000;
        private static final int ASYNC_MSG_RESPONSE_SUCCESS = 0x0001;
        private static final int ASYNC_MSG_CANCEL_SEND = 0x0002;
        private static final int ASYNC_MSG_NETWORK_EXCEPTION = 0x0004;
        private static final int ASYNC_MSG_OTHER_EXCEPTION = 0x0008;

        AsyncResponseHandlerProxy(Looper looper) {
            super(looper);
        }

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case ASYNC_MSG_SEND_REQUEST:
                    onAsyncSendRequest(msg.arg1);
                    break;

                case ASYNC_MSG_RESPONSE_SUCCESS:
                    onAsyncResponseSuccess(msg.arg1, (String) msg.obj);
                    break;

                case ASYNC_MSG_CANCEL_SEND:
                    onAsyncCancel(msg.arg1);
                    break;

                case ASYNC_MSG_NETWORK_EXCEPTION:
                    onAsyncNetworkException(msg.arg1, (Exception) msg.obj);
                    break;

                case ASYNC_MSG_OTHER_EXCEPTION:
                    onAsyncException(msg.arg1, (Exception) msg.obj);
                    break;

                default:
                    onAsyncException(msg.arg1, new IllegalArgumentException("This condition branch is undefined."));
            }
        }

        @Override
        public void onSendRequest(int flag) {
            sendMessage(obtainMessage(ASYNC_MSG_SEND_REQUEST, flag, flag));
        }

        @Override
        public void onResponseSuccess(int flag, String response) {
            sendMessage(obtainMessage(ASYNC_MSG_RESPONSE_SUCCESS, flag, flag, response));
        }

        @Override
        public void onCancel(int flag) {
            sendMessage(obtainMessage(ASYNC_MSG_CANCEL_SEND, flag, flag));
        }

        @Override
        public void onException(int flag, Exception ex) {
            sendMessage(obtainMessage(ASYNC_MSG_OTHER_EXCEPTION, flag, flag, ex));
        }

        @Override
        public void onNetworkException(int flag, Exception ex) {
            sendMessage(obtainMessage(ASYNC_MSG_NETWORK_EXCEPTION, flag, flag, ex));
        }
    }


}
